'use strict';

let { user } = require("../models");

let homePage_fn = async (ctx, next) => {
    ctx.render('homePage.html');

    let username = ctx.session.userName;
    console.log("初始界面：" + username);

    if (!username) {
        ctx.redirect('/login');
    } else {
        await next()
    }


    console.log(username);
}

let login_fn = async (ctx, next) => {
    ctx.render('login.html');

    let username = ctx.session.userName;
    console.log("登录界面：" + username);

    if (username) {
        ctx.redirect('/');
    }
}

let register_fn = async (ctx, next) => {
    ctx.render('register.html');
}


//登录判断
let loginDone_fn = async (ctx, next) => {

    let username = ctx.request.body.username || '';
    let password = ctx.request.body.password || '';


    let user1 = await user.findOne({ where: { username: username, password: password } })
    if (user1 !== 'null') {


        ctx.session.userName = await user.findOne({where:{username:username}});
        // ctx.session.userId = await user.findOne({where:{userId:userId}});

        // console.log(ctx.session.userId);
        // console.log( ctx.session.userName);

        ctx.redirect('/');
    } else {
        ctx.body = '用户名或密码错误';
    }
    // let up = await user.findOne({where:{username:username,password:password}}).then((row)=>{
    //     let res;
    //     let inf = JSON.stringify(up);
    //     let u2 = JSON.parse(inf);
    //     console.log(inf);
    //     console.log(u2);
    //     if(inf !== 'null'){
    //         ctx.session.userId = row.id;
    //         ctx.session.userName = row.username;
    //         res={code:200,msg:'登录成功'}
    //     }else{
    //         res={code:1000,msg:'用户名或密码错误'}
    //     }
    // });
    // ctx.body = res;
}


//注册判断
let registerDone_fn = async (ctx, next) => {
    let username = ctx.request.body.username || '';
    let password = ctx.request.body.password || '';
    let confirmPassword = ctx.request.body.confirmPassword || '';

    let uname = await user.findOne({ where: { username: username } });

    if (username.length > 0 && password.length > 0 && password === confirmPassword) {

        // let userDemo = await user.findOne({where:{username:username}});
        // if(userDemo){
        //     ctx.body = '用户名已存在';
        // }else{
        //     await user.create({ username: username, password: password });
        //     ctx.redirect('/login');
        // }
        // try {
        //     await user.create({ username: username, password: password });
        //     ctx.redirect('/login');
        // } 
        // catch (error) {
        //     ctx.body = '用户名已存在';
        //     console.log('注册失败'+error);
        // }
        if (username !== uname) {
            await user.create({ username: username, password: password });

            ctx.redirect('/login');
        } else {
            ctx.body = '用户名已存在';
        }
    } else {
        ctx.body = '用户名或密码不能为空，并且两次密码应该一致'
    }

}

//注销
let logout_fn = async (ctx, next) => {
    ctx.redirect('/help');
}

let help_fn = async (ctx, next) => {
    // let userName = ctx.session.userName;
    //  console.log("注销界面："+userName);

    // if(userName === "ok"){
    //     ctx.session = null;
    //     ctx.redirect('/login');
    // }

    ctx.session = null;
    ctx.redirect('/login');
}

module.exports = {
    '/': ['get', homePage_fn],
    '/login': ['get', login_fn],
    '/register': ['get', register_fn],
    '/loginDone': ['post', loginDone_fn],
    '/registerDone': ['post', registerDone_fn],
    '/logout': ['get', logout_fn],
    '/help': ['get', help_fn]
}